<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Http\Requests\Order\OrderRequest;
use Illuminate\Http\Request;
use App\Model\Product;
class ProductController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_xProduct'=>'am-in',
            '_product'=>'am-active',
        ]);
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {

        $products=Product::orderBy('sort_order')->paginate(config('xSystem.page_size'));
        return view('Admin.xHotel.product.index')->with('products',$products);

    }

    /**
     * 新增显示页
     */
    public function create(){

        return view('Admin.xHotel.product.create');

    }

    /**
     * 新增
     * @param OrderRequest $request
     */
    public function store(OrderRequest $request)
    {
        Product::create($request->all());
        return redirect('/product')->with('success', '添加成功');
    }

    /**
     * 修改显示页
     * @param $id
     */
    public function edit($id)
    {
        $product = Product::find($id);
        return view('Admin.xHotel.product.edit')->with('product',$product);
    }

    /**
     * 修改
     * @param OrderRequest $request
     * @param $id
     */
    public function update(OrderRequest $request, $id)
    {
        $product = Product::find($id);
        $product->update($request->all());
        return redirect('/product')->with('success', '编辑成功');
    }

    /**
     * 删除
     * @param $id
     */
    public function destroy($id)
    {
        Product::destroy($id);
        return back()->with('success', '删除成功');

    }

    /**
     * 排序
     * @param Request $request
     */
    public function sort_order(Request $request){
       
        $sort_order=$request->sort_order;
        $category = Product::find($request->id);
        $category->sort_order=$sort_order;
        $category->save();
    }

    /**
     * 是否显示
     * @param Request $request
     */
    public function is_something(Request $request)
    {
        $attr = $request->attr;
        $hotelreferral = Product::find($request->id);
        $value=$hotelreferral->$attr ? false : true;
        $hotelreferral->$attr=$value;
        $hotelreferral->save();
    }


}
